package com.dyb.recuitment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dyb.recuitment.entity.User;
import com.dyb.recuitment.mapper.UserMapper;
import com.dyb.recuitment.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 邓宇斌
 * @since 2022-01-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public User getByUser(User user) {
        user.setStatus(1);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>(user);
        List<User> result = userMapper.selectList(queryWrapper);
        if (result.size() > 0) {
            return result.get(0);
        }
        return null;
    }

    @Override
    public int updateUser(User user) {
        LocalDateTime localDateTime = LocalDateTime.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), df);
        user.setUpdateTime(localDateTime);
        return userMapper.updateById(user);
    }


    @Override
    public int updatePwd(User user) {
        LocalDateTime localDateTime = LocalDateTime.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), df);
        String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(password).setUpdateTime(localDateTime);
        return userMapper.updateById(user);
    }

    @Override
    public int deleteUser(User user) {
        LocalDateTime localDateTime = LocalDateTime.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), df);
        user.setStatus(0).setUpdateTime(localDateTime);
        ;
        return userMapper.updateById(user);
    }

    @Override
    public List<User> selectAllUser() {
        return userMapper.selectList(new QueryWrapper<>());
    }
}
